package com.wgj.android.accesslibility.service;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.SystemClock;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;

import com.jiaai.xui.utils.log.XLog;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.accesslibility.IStatusBarNotification;
import com.wgj.android.accesslibility.config.WXConfginUtil;
import com.wgj.android.accesslibility.config.WXQiangHongBaoConfig;
import com.wgj.android.accesslibility.job.AccessbilityJob;


import com.wgj.android.accesslibility.job.AddFriendsByGroupJob;
import com.wgj.android.accesslibility.job.AddFriendsByLocationJob;
import com.wgj.android.accesslibility.job.QHBAccessbilityJob;
import com.wgj.android.accesslibility.job.WXCleanFriendsJob;
import com.wgj.android.ui.activity.CleanFriednsActivity;
import com.wgj.android.ui.activity.WXHongBaoActivity;
import com.wgj.android.web.entity.WebConfig;
import com.wgj.android.zfp.WXCleanService;

import static com.wgj.android.accesslibility.job.AccessbilityJob.WECHAT_PACKAGENAME;

/**
 * Created by Administrator on 2017/12/8.
 */

public class WGJAccessServier extends AccessibilityService {
    private static  final  String TAG="WGJAccessServier";
    private  static PackageInfo mWechatPackageInfo;
    private   AddFriendsByLocationJob addFriendsByLocationJob;
    private AddFriendsByGroupJob addFriendsByGroupJob;
    private static QHBAccessbilityJob qhbAccessbilityJob;
    private WXCleanFriendsJob cleanFriendsJob;


    @Override
    protected void onServiceConnected() {

        updatePackageInfo();
        createQHBJob();
        if(cleanFriendsJob!=null){
            cleanFriendsJob.onStopJob();
            cleanFriendsJob=null;
        }
        if(addFriendsByGroupJob!=null){
            addFriendsByGroupJob.onStopJob();
            addFriendsByGroupJob=null;
        }
        if(addFriendsByLocationJob!=null){
            addFriendsByLocationJob.onStopJob();
            addFriendsByLocationJob=null;
        }
        Notification.Builder builder = new Notification.Builder(this.getApplicationContext()); //获取一个Notification构造器
        Intent nfIntent = new Intent(this, CleanFriednsActivity.class);
        builder.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0)) // 设置PendingIntent
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.mipmap.logo)) // 设置下拉列表中的图标(大图标)
                .setContentTitle("旺旺通") // 设置下拉列表里的标题
                .setSmallIcon(R.mipmap.logo) // 设置状态栏内的小图标
                .setContentText("旺旺通缩合助手") // 设置上下文内容
                .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间

        Notification notification = builder.build(); // 获取构建好的Notification
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
        startForeground(110, notification);
        if(!isNotificationServiceRunning()){
            toggleNotificationListenerService();
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
            Log.e(TAG,event.getClassName().toString());
        if(event.getEventType()==AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED){
            Log.e(TAG,"WXQHBNotificationService isRunning:" +WXQHBNotificationService.isRunning());
            if(!WXQHBNotificationService.isRunning()  && WXConfginUtil.isOpenQhb()){
                qhbAccessbilityJob.onReceiveJob(event);

            }
            return;
        }

            if(WXConfginUtil.isOpenCleanFirend()){//是否开启了检测僵尸粉
                if(cleanFriendsJob==null){
                    createCleanFriendsJob();
                }
                cleanFriendsJob.onReceiveJob(event);
                return;
            }
            if(WXConfginUtil.isOpenAddFriendsByGropu()){
                if(addFriendsByGroupJob==null){
                    createAddGroupFriendsJob();
                }
                addFriendsByGroupJob.onReceiveJob(event);
                return;
            }
            if(WXConfginUtil.isOpenAddFriendByLocation()){
                if(addFriendsByLocationJob==null){
                   createAddLocationFriendsJob();
                }
               addFriendsByLocationJob.onReceiveJob(event);
               return;
            }
            if(WXConfginUtil.isOpenQhb()){
                if(qhbAccessbilityJob==null){
                    createQHBJob();
                }
                qhbAccessbilityJob.onReceiveJob(event);
                return;
            }





    }

    @Override
    public void onInterrupt() {
        Log.i(TAG,"旺旺通综合小助手被中断啦");
        XToast.error("旺旺通综合小助手已被中断啦!");
    }

    /**
     * 处理通知栏信息
     * @param notificationService
     */
    public static void handeNotificationPosted(final IStatusBarNotification notificationService) {
        if(WXConfginUtil.isOpenQhb() && !WXConfginUtil.isOpenAddFriendByLocation() && !WXConfginUtil.isOpenAddFriendsByGropu()){
            if(qhbAccessbilityJob!=null ){
                qhbAccessbilityJob.onNotificationPosted(notificationService);
            }

        }
    }


    private void updatePackageInfo() {
        try {
            mWechatPackageInfo =getBaseContext().getPackageManager().getPackageInfo(WECHAT_PACKAGENAME, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
    /** 获取微信的版本*/
    private static int getWechatVersion() {
        if(mWechatPackageInfo == null) {
            return 0;
        }
        XLog.i(mWechatPackageInfo.versionCode+"");

        return  mWechatPackageInfo.versionCode;
    }

    /** 快速读取通知栏服务是否启动*/
    public static boolean isNotificationServiceRunning() {
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            return false;
        }
        //部份手机没有NotificationService服务
        try {
            return WXQHBNotificationService.isRunning();
        } catch (Throwable t) {}
        return false;
    }

    private void toggleNotificationListenerService() {
        PackageManager pm = getPackageManager();
        pm.setComponentEnabledSetting(new ComponentName(this, WXQHBNotificationService.class),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

        pm.setComponentEnabledSetting(new ComponentName(this, WXQHBNotificationService.class),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

    }

    @Override
    public void onDestroy() {
        mWechatPackageInfo=null;
        if(cleanFriendsJob!=null){
            cleanFriendsJob.onStopJob();
        }
        if(addFriendsByGroupJob!=null){
            addFriendsByGroupJob.onStopJob();
        }
        if(addFriendsByLocationJob!=null){
            addFriendsByLocationJob.onStopJob();
        }
        WXConfginUtil.closeAddFriendsByGropu();
        WXConfginUtil.closeAddFriendByLocation();
        WXConfginUtil.closeQhb();
        stopForeground(true);// 停止前台服务--参数：表示是否移除之前的通知
        super.onDestroy();

    }

    private  void  createAddGroupFriendsJob(){
        addFriendsByGroupJob=new AddFriendsByGroupJob();
        addFriendsByGroupJob.onCreateJob(this);
        addFriendsByGroupJob.setWeiXingVersion(getWechatVersion());
    }

    private  void createQHBJob(){
        qhbAccessbilityJob=new QHBAccessbilityJob();
        qhbAccessbilityJob.onCreateJob(this);
        qhbAccessbilityJob.setWeiXingVersion(getWechatVersion());
    }
    private void createCleanFriendsJob(){
        cleanFriendsJob=new WXCleanFriendsJob();
        cleanFriendsJob.onCreateJob(this);
        cleanFriendsJob.setWeiXingVersion(getWechatVersion());
    }
    private void createAddLocationFriendsJob(){
        addFriendsByLocationJob=new AddFriendsByLocationJob();
        addFriendsByLocationJob.onCreateJob(this);
        addFriendsByLocationJob.setWeiXingVersion(getWechatVersion());
    }
    public void  stopAddGroupFriendsJob(){
        addFriendsByGroupJob=null;
    }
    public void  stopAddLocationFriendsJob(){
        addFriendsByLocationJob=null;
    }
    public void  stopCleanJob(){
        cleanFriendsJob=null;
    }


    public static boolean  isRun(){
        return mWechatPackageInfo!=null;

    }
}
